package com.hankol.winecellar.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.baselibrary.utils.GlideUtils;
import com.baselibrary.view.RoundCornerImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hankol.winecellar.R;
import com.baselibrary.view.GlideRoundTransform;

import java.util.ArrayList;

public class UploadImageVideoAdapter extends RecyclerView.Adapter<UploadImageVideoAdapter.ImageVideoHolder> {
    private Context mContext;
    private ArrayList<String> mList;
    private int mDynamictype;//1图文；2视频
    private LayoutInflater mInflater;
    private OnItemClickListener mOnItemClickListener;
    private ItemTouchHelper mItemHelper;
    //item的大小对应不同的item布局
    private int itemSizeStyle = 0;

    public UploadImageVideoAdapter(Context mContext, int itemSizeStyle, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
        this.itemSizeStyle = itemSizeStyle;
        mInflater = LayoutInflater.from(mContext);
    }

    public ArrayList<String> getListData() {
        return mList;
    }

    public void setDynamictype(int dynamictype) {
        this.mDynamictype = dynamictype;
    }

    public void setData(ArrayList<String> list) {
        this.mList = list;
    }

    public void addData(ArrayList<String> list) {
        this.mList.addAll(list);
    }

    public void setOnClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setItemHelper(ItemTouchHelper mItemHelper) {
        this.mItemHelper = mItemHelper;
    }

    @NonNull
    @Override
    public UploadImageVideoAdapter.ImageVideoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        int layRes = R.layout.item_upload_image_and_video;
        if (itemSizeStyle == 0) {
            layRes = R.layout.item_upload_image_and_video;
        } else {
            layRes = R.layout.item_upload_imagevideo78;
        }
        return new UploadImageVideoAdapter.ImageVideoHolder(mInflater.inflate(layRes, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull UploadImageVideoAdapter.ImageVideoHolder imageVideoHolder, int position) {
        if (position < mList.size()) {
            //代表+号之前的需要正常显示图片
            if (position == 0) {//封面
                imageVideoHolder.mIvCoversType.setVisibility(mDynamictype == 2 ? View.VISIBLE : View.GONE);
                imageVideoHolder.mLlCover.setVisibility(View.VISIBLE);
            } else {
                imageVideoHolder.mIvCoversType.setVisibility(View.GONE);
                imageVideoHolder.mLlCover.setVisibility(View.GONE);
            }
            String picUrl = mList.get(position); //图片路径
//            RequestOptions userAvatarOptions = new RequestOptions().signature(new ObjectKey(System.currentTimeMillis()));
//            Glide.with(mContext).applyDefaultRequestOptions(userAvatarOptions).load(picUrl).into(imageVideoHolder.mIvFriendlistHead);
            GlideUtils.loadImg_101(mContext,picUrl,imageVideoHolder.mIvFriendlistHead);
            if (mList.size() >= 1) {
                imageVideoHolder.mIvDelete.setVisibility(View.VISIBLE);
            } else {
                imageVideoHolder.mIvDelete.setVisibility(View.GONE);
            }
            imageVideoHolder.listener(imageVideoHolder, position);
        } else {
            Glide.with(mContext)
                    .load(itemSizeStyle == 0 ? R.drawable.ic_upload_temp_3 : R.drawable.ic_upload_temp_4)
                    .centerCrop()
                    .placeholder(R.drawable.placeholder)
                    .error(R.drawable.placeholder)
                    .transform(new GlideRoundTransform(mContext, 8))
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageVideoHolder.mIvFriendlistHead);
            imageVideoHolder.mLlCover.setVisibility(View.GONE);
            imageVideoHolder.mIvDelete.setVisibility(View.GONE);
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                //拖拽Item 这里不添加任何点击事件。。但是不实现长按监听，则默认执行拖拽方法，所以这里面什么都不做
                return false;
            });
            imageVideoHolder.mIvFriendlistHead.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onAddItemClick();
            });
        }

    }


    @Override
    public int getItemCount() {
        int count = mList == null || mList.size() == 0 ? 1 : mList.size() + 1;
        if (mDynamictype == 1 && count > 9) {
            return mList.size();
        } else if (mDynamictype == 2) {
            return 1;
        } else {
            return count;
        }
    }


    public class ImageVideoHolder extends RecyclerView.ViewHolder {
        private RoundCornerImageView mIvFriendlistHead;
        private ImageView mIvCoversType;
        private LinearLayout mIvDelete;
        private LinearLayout mLlCover;

        public ImageVideoHolder(@NonNull View itemView) {
            super(itemView);
            mIvFriendlistHead = itemView.findViewById(R.id.pic_iv);
            mIvDelete = itemView.findViewById(R.id.iv_delete);
            mLlCover = itemView.findViewById(R.id.ll_cover);
            mIvCoversType = itemView.findViewById(R.id.iv_covers_type);
        }

        private void listener(@NonNull ImageVideoHolder imageVideoHolder, int position) {
            imageVideoHolder.mLlCover.setOnClickListener(v -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onVideoCoversClick();
            });
            imageVideoHolder.mIvFriendlistHead.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onItemClick(position, mList);
            });
            imageVideoHolder.mIvFriendlistHead.setOnLongClickListener(view -> {
                if (mItemHelper != null)
                    mItemHelper.startDrag(imageVideoHolder);
                return false;
            });
            imageVideoHolder.mIvDelete.setOnClickListener(view -> {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onDeleteItemClick(position, mList);
            });
        }
    }


    public interface OnItemClickListener {
        void onItemClick(int pos, ArrayList<String> list);

        void onAddItemClick();

        void onDeleteItemClick(int pos, ArrayList<String> list);

        void onVideoCoversClick();
    }
}

